/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/08/22
 */

#include <QCoreApplication>

#include "GameEngine.h"

template<> GameEngine* Singleton<GameEngine>::ms_Singleton = 0;

GameEngine::GameEngine ( QObject* rParent ) : QObject (rParent) {
}

GameEngine::~GameEngine() {
}

void GameEngine::startup () {

	mLogManager = new LogManager ();
	mConfigManager = new ConfigManager ();
	mRenderManager = new RenderManager ();
	mInputManager = new InputManager ();
	mGUIManager = new GUIManager ();
	mSoundManager = new SoundManager ( this );
	mStateManager = new StateManager ();
	mScriptManager = new ScriptManager ();

	mConfigManager->init();
	mLogManager->init();
	mRenderManager->init();
	mInputManager->init();
	mGUIManager->init();
	mSoundManager->init();
	mScriptManager->init();
	mStateManager->init();
	mStateManager->start( QThread::InheritPriority );
}

void GameEngine::shutdown() {

	mStateManager->shutdown();
	mScriptManager->shutdown();
	mSoundManager->shutdown();
	mGUIManager->shutdown();
	mInputManager->shutdown();
	mRenderManager->shutdown();
	mLogManager->shutdown();
	mConfigManager->shutdown();

	mStateManager->quit();
}

void GameEngine::finish () {
    QCoreApplication* app = QCoreApplication::instance();
    shutdown();
	delete mConfigManager;
	delete mInputManager;
	delete mGUIManager;
	delete mSoundManager;
	delete mLogManager;
	delete mStateManager;
	delete mScriptManager;
	delete mRenderManager;
	app->exit(0);
}

GameEngine* GameEngine::getSingletonPtr(void) {
	return ms_Singleton;
}

GameEngine& GameEngine::getSingleton(void) {
	assert( ms_Singleton );  return ( *ms_Singleton );
}


